package com.nyt.report.service.impl;

import com.alibaba.fastjson.JSON;
import com.nyt.report.model.ApiUser;
import com.nyt.report.model.SysApiStation;
import com.nyt.report.service.CacheService;
import com.nyt.report.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CacheServiceImpl implements CacheService {



    @Value("${cache.key.prefix}")
    String KEY_PREFIX;

    @Autowired
    JCacheCacheManager cacheManager;

    @Autowired
    //@Qualifier("firstJdbcTemplate")
    JdbcTemplate firstJdbcTemplate;

    @Override
    public Object save(String cache, Object key, Object val) {
        // insert操作
        cacheManager.getCache(cache).put(key, val);
        return key;
    }

    @Override
    public Object update(String cache, Object key, Object val) {
        // 更新操作
        cacheManager.getCache(cache).putIfAbsent(key, val);
        return key;
    }

    @Override
    public void delete(String cache, Object key) {
        // 删除操作
        cacheManager.getCache(cache).evict(key);
    }

    @Override
    public Object search(String cache, Object key, Class clazz) {
        // 查询操作
        Object obj = cacheManager.getCache(cache).get(key, clazz);
        return obj;
    }

    @Override
    public ApiUser queryApiUser(String accesskey) {
        String cacheKey = KEY_PREFIX + accesskey;
        String json = cacheManager.getCache("ApiUserCache").get(cacheKey, String.class);

//        if (!StringUtils.isBlank(json)) {
//            try {
//                json = cacheManager.getCache("ApiUserCache").boundValueOps(cacheKey).get().toString();
//            }catch (Exception e) {
//                e.printStackTrace();
//            }
//        }

        if (StringUtils.isBlank(json)) {
            String sql = "select * from sys_api_user where accesskey='"+accesskey+"' and is_delete = 0";
            List<ApiUser> users = firstJdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ApiUser.class));
            if (users == null || users.size()<=0){
                return null;
            }else {
                // 15分钟会更新一次
                json = JSON.toJSONString(users.get(0));
                try {
                    cacheManager.getCache("ApiUserCache").put(cacheKey, json);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                return users.get(0);
            }
        }
        return JSON.parseObject(json, ApiUser.class);
    }

    @Override
    public SysApiStation getStationWhitelist(String IPAddr) {
        String sql = "SELECT * FROM sys_api_station where ip = '" + IPAddr+"'and is_delete = 0";
        List<SysApiStation> sysApiStation = firstJdbcTemplate.query(sql,new BeanPropertyRowMapper<>(SysApiStation.class));
        return sysApiStation.get(0);
    }


    /*@Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    @Value("${station.config.cachetime}")
    Integer cacheTime;

    @Override
    public StationConfig queryStationConfig(String accesskey) {
        String json = (String) redisTemplate.opsForValue().get(accesskey);
        if (StringUtils.isBlank(json)) {
            String sql = "select * from sys_api_user where accesskey='"+accesskey+"' and is_enable = 1";
            List<StationConfig> station = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(StationConfig.class));
            if (station == null || station.size()<=0){
                return null;
            }else {
                // 15分钟会更新一次
                json = JSON.toJSONString(station.get(0));
                redisTemplate.opsForValue().set(accesskey, json, Duration.ofMinutes(cacheTime));
                return station.get(0);
            }
        }
        return JSON.parseObject(json, StationConfig.class);
    }

    @Override
    public Boolean exitRequestid(String requestid, String ip) {
        String ipAddr = (String) redisTemplate.opsForValue().get(requestid);
        if (StringUtils.isBlank(ipAddr)) {
            redisTemplate.opsForValue().set(requestid, ip, Duration.ofMinutes(30));
            return false;
        }
        return true;
    }*/
}
